﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using sdamarketing.App_Code;

namespace sdamarketing.Account
{
    public partial class ForgotPassword : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PanelPassword.Enabled = false;
                PanelPassword.Visible = false; // Panelot ke se pokaze i ke bide Enabl samo ako odgovorot e tocen

                PanelQuestion.Enabled = false;
                PanelQuestion.Visible = false;
            }

        }

        SqlConnection connection = ConnectionMenager.GetConnection();

        protected void ShowQuestion_Click(object sender, EventArgs e)  
        {
            // Ova se komandite od Finduser , modificirani za potrebite na ovoj metod
            string sql = "SELECT username,securityQuestion FROM Users WHERE username = " + "'" + username.Text.ToString() + "' AND isDeleted = '0' AND isBlock = '0'";

            SqlCommand command = new SqlCommand(sql, connection);
            command.CommandType = CommandType.Text;

            SqlDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult);

            if (username.Text == "")
            {
                if (Session["MyCulture"].ToString() == "en-US")
                {
                    lblUser.Text = "Please enter username.";
                }
                else
                {
                    lblUser.Text = "Ве молиме внесете корисничко име.";
                }
                
            }
            else
            {
                reader.Read();
                try
                {
                    if (Session["MyCulture"].ToString() == "en-US")
                    {
                        lblSecirityQuestion.Text = "Question : " + reader.GetString(1); // GetString(1) ja  dava vtorata kolona od Sql Kverito t.e. prasanjeto
                    }
                    else
                    {
                        lblSecirityQuestion.Text = "Прашање : " + reader.GetString(1); // GetString(1) ja  dava vtorata kolona od Sql Kverito t.e. prasanjeto
                    }
                    
                    if (username.Text == reader.GetString(0))
                    {
                        if (Session["MyCulture"].ToString() == "en-US")
                        {
                            lblUser.Text = "User found.";
                        }
                        else
                        {
                            lblUser.Text = "Корисникот е пронајден.";
                        }
                        
                        PanelQuestion.Enabled = true;// Vkluci go  panelot t.e. da bide enabled i pokazi go
                        PanelQuestion.Visible = true;
                        PanelUsername.Enabled = false;
                    }
                    else
                    {
                        if (Session["MyCulture"].ToString() == "en-US")
                        {
                            lblUser.Text = "The username is already in use.";
                        }
                        else
                        {
                            lblUser.Text = "Корисник со такво корисничко име(username) не постои.";
                        }
                        
                    }
                    //IDUser = reader.GetInt32(0);
                    //IDUser = int.Parse(reader[0].ToString());

                }
                catch (Exception exception)
                {
                    if (Session["MyCulture"].ToString() == "en-US")
                    {
                        lblUser.Text = "Error, try again.";
                    }
                    else
                    {
                        lblUser.Text = "Грешка , обидете се повторно.";
                    }
                   
                    SendToLogFile.SendRecordToFIle(DateTime.Now.ToString() + " -exc- " + exception.Message.ToString() + " -stackTrace- " + exception.StackTrace.ToString() + " -ForgotPassword- ");

                }
            }
            reader.Close();
            //connection.Close();
            //return IDUser;
        }

        protected void btnCheck_Click(object sender, EventArgs e)
        {
            string sql = "SELECT securityAnswer FROM Users WHERE username = " + "'" + username.Text.ToString() + "'";

            SqlCommand command = new SqlCommand(sql, connection);
            command.CommandType = CommandType.Text;

            SqlDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult);

            //int IDUser = 0;

            if (txbAnswer.Text == "")
            {
                if (Session["MyCulture"].ToString() == "en-US")
                {
                    lblOdgovor.Text = "Please enter answer.";
                }
                else
                {
                    lblOdgovor.Text = "Ве молиме внесете одговор.";
                }
            }
            else
            {
                reader.Read();
                try
                {
                    //IDUser = reader.GetInt32(0);
                    //IDUser = int.Parse(reader[0].ToString());

                    if (txbAnswer.Text == reader.GetString(0))
                    {
                        if (Session["MyCulture"].ToString() == "en-US")
                        {
                            lblOdgovor.Text = "The answer is correct.";
                        }
                        else
                        {
                            lblOdgovor.Text = "Одговорот е точен.";
                        }
                        
                        PanelPassword.Enabled = true;// Vkluci go  panelot t.e. da bide enabled i pokazi go
                        PanelPassword.Visible = true;
                        PanelQuestion.Enabled = false;
                        lblUser.Text = "";
                    }
                    else
                    {
                        if (Session["MyCulture"].ToString() == "en-US")
                        {
                            lblOdgovor.Text = "Answer is incorrect,try again.";
                        }
                        else
                        {
                            lblOdgovor.Text = "Одговорот не е точен, обидете се повторно.";
                        }   
                    }

                }
                catch (Exception exception)
                {
                    if (Session["MyCulture"].ToString() == "en-US")
                    {
                        lblOdgovor.Text = "Error, try again.";
                    }
                    else
                    {
                        lblOdgovor.Text = "Грешка, обидете се повторно.";
                    }  
                    
                    SendToLogFile.SendRecordToFIle(DateTime.Now.ToString() + " -exc- " + exception.Message.ToString() + " -stackTrace- " + exception.StackTrace.ToString() + " -ForgotPassword- ");
                }
            }

            reader.Close();
            //connection.Close();
        }

        protected void btnSavePassword_Click(object sender, EventArgs e)
        {
          DateTime dateModify = DateTime.Now;
          string pasvord = PasswordTextBox.Text;
          string juzernejm = username.Text;

          SqlParameter pasvordParam = new SqlParameter("@pasvordParam", pasvord);
          SqlParameter juzernameParam = new SqlParameter("@juzernameParam", juzernejm);
          SqlParameter dateModifyParam = new SqlParameter("@dateModifyParam", dateModify);

          string sql = @"UPDATE Users SET password =@pasvordParam,dateModify =@dateModifyParam WHERE username=@juzernameParam";

          SqlCommand command = new SqlCommand(sql, connection);

          command.Parameters.Add(pasvordParam);
          command.Parameters.Add(juzernameParam);
          command.Parameters.Add(dateModifyParam);

          if (PasswordTextBox.Text == "")
          {
              if (Session["MyCulture"].ToString() == "en-US")
              {
                  lblPass.Text = "Please enter password.";
              }
              else
              {
                  lblPass.Text = "Ве молиме внесете лозинка.";
              }     
          }
          else
          {
              try
              {
                  command.ExecuteNonQuery();

                  if (PasswordTextBox.Text == "")
                  {
                      if (Session["MyCulture"].ToString() == "en-US")
                      {
                          lblPass.Text = "Please enter password.";
                      }
                      else
                      {
                          lblPass.Text = "Ве молиме внесете лозинка.";
                      }  
                  }

                  if (PasswordTextBox.Text == PasswordConfTextBox.Text)
                  {
                      if (Session["MyCulture"].ToString() == "en-US")
                      {
                          lblPass.Text = "Password changed.";
                      }
                      else
                      {
                          lblPass.Text = "Лозинката е успешно променета.";
                      }  
                      
                      linkce.Visible = true;
                      PanelPassword.Enabled = false;
                      PanelPassword.Visible = false;
                      PanelQuestion.Enabled = false;
                      PanelQuestion.Visible = false;

                  }
                  else
                  {
                      if (Session["MyCulture"].ToString() == "en-US")
                      {
                          lblPass.Text = "Passwords dont match.";
                      }
                      else
                      {
                          lblPass.Text = "Грешка , внесете иста вредност во двете полиња.";
                      }  
                      
                  }

              }
              catch (Exception exception)
              {
                  if (Session["MyCulture"].ToString() == "en-US")
                  {
                      lblPass.Text = "Error,try again.";
                  }
                  else
                  {
                      lblPass.Text = "Грешка, обидете се повторно.";
                  }  
                  
                  SendToLogFile.SendRecordToFIle(DateTime.Now.ToString() + " -exc- " + exception.Message.ToString() + " -stackTrace- " + exception.StackTrace.ToString() + " -ForgotPassword- ");
              }
              connection.Close();
          }
        }
    }
}
